package com.happyscan138.makefun;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.happyscan138.makefun.Urls.Urls;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.happyscan138.makefun.base.share.access_key;

/**
 * Created by Administrator on 2017/1/7.
 */

public class FeedBack extends AppCompatActivity implements View.OnClickListener {
    private ImageButton back;
    private EditText editTextFeedback;
    private String feedback;
    private Button input;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback);
        preferences = this.getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView();
    }

    public void initView() {
        TextChange textChange=new TextChange();
        back = (ImageButton) findViewById(R.id.feedback_back);
        back.setOnClickListener(this);
        editTextFeedback = (EditText) findViewById(R.id.feedback_text);
        editTextFeedback.addTextChangedListener(textChange);
        input=(Button)findViewById(R.id.feedback_input);
        input.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.feedback_back:
                finish();
                break;
            case R.id.feedback_input:
                client=new AsyncHttpClient();
                params=new RequestParams();
                params.put("access_key",access_key);
                params.put("feedback",feedback);
                RequestHandle handle=client.post(Urls.Feedback,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            int i=jsonObject.getInt("code");
                            String msg=jsonObject.getString("msg");
                            if(i==1){
                                Toast.makeText(FeedBack.this,msg,Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
    }

    //EditText的监听器
    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            feedback = editTextFeedback.getText().toString();

        }
    }
}